package com.cdut.logistics.action.datacentre;

import java.util.ArrayList;
import java.util.List;

import com.cdut.logistics.intercepter.system.AuthorityIntercepter;
import com.cdut.logistics.model.dao.datacentre.School;
import com.cdut.logistics.service.impl.datacentre.SchoolImpl;
import com.cdut.logistics.service.interf.datacentre.SchoolService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

/**  
 * @ClassName: UserController  
 * @Description: 学校管理控制类
 * @author wxt.xia
 * @date 2018年1月26日 下午3:28:25    
 */
//@Before(AuthorityIntercepter.class)
public class SchoolController extends Controller {
	
	private SchoolService schoolService = new SchoolImpl();
	
	
	
	/**
	 * 
	 * @Title: queryById  
	 * @Description: 根据学校id查询学校
	 * @author wxt.xia
	 * @param json
	 * @throws
	 */
	public void queryById(){
		/*
		 * 解析json
		 */
		int school_id = Integer.valueOf((String) getPara("school_id"));
		Record school = new Record();
		try {
			school = schoolService.queryById(school_id);
			System.out.println(school.toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		renderJson(school);
	}
	
	/**
	 * 
	 * @Title: query  
	 * @Description: 根据学校名称查询学校
	 * @author wxt.xia
	 * @param json
	 * @throws
	 */
	public void query(){
		/*
		 * 解析json
		 */
		String school_name = getPara("school_name")+"%";
		List<Record> schoolList = new ArrayList<Record>();
		try {
			schoolList = schoolService.query(school_name);
			System.out.println(schoolList.get(0).toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		renderJson(schoolList);
	}
	
	/**
	 * 
	 * @Title: add  
	 * @Description: 新增学校
	 * @author wxt.xia
	 * @throws
	 */
	public void add(){
		/*
		 * 解析json
		 */
		
		//下面是测试数据，id需设置为自增长
		School school = new School();
		school.set("name", "成都理工大学");
		school.set("province", 1);
		school.set("city", 1);
		school.set("area", 1);
		try {
			System.out.println("控制类*************");
			schoolService.add(school);
		} catch (Exception e) {
			
		}
	}
	
	/**
	 * 
	 * @Title: form  
	 * @Description: 修改学校
	 * @author wxt.xia
	 * @throws
	 */
	public void modify(){
		/*
		 * 解析json
		 */
		School school = new School();
		school.set("name", "电子科技大学");
		school.set("id", 2);
		School dataSchool = schoolService.get((Integer)school.get("id"));
		dataSchool.set("name",school.get("name"));
		try {
			schoolService.modify(dataSchool);
		} catch (Exception e) {
			
		}
	}
	
	/**
	 * 
	 * @Title: remove  
	 * @Description: 删除学校
	 * @author wxt.xia
	 * @throws
	 */
	public void remove(){
		int id = 3;
		try {
			schoolService.remove(id);
		} catch (Exception e) {
			
		}
	}
}
